from django.shortcuts import render
from django.views import View
from apps.areas.models import Area
from django.http import JsonResponse
from django.core.cache import cache
# Create your views here.


class AreaView(View):

    def get(self, request):
        """
        获取市区数据信息
        :return:
        """
        # 读取缓存
        user = request.user
        area_list = cache.get('area')

        areas = Area.objects.filter().all()
            # 将市区信息查询结果转换为字典列表
        area_list = []
        for item in areas:
            area_list.append({
                'aid': item.id,
                'aname': item.name
            })
            # 设置缓存
            cache.set('area', area_list, 24*3600)
        # 返回响应
        return JsonResponse({"errno": "0", "errmsg": "获取成功", "data": area_list})


